<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\AccountKeep as AccountKeepModel;
use app\common\model\AccountCate as AccountCateModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;

/**
 * 记账控制器
 * Class Accountcate
 * @package app\home\controller
 */
class Accountkeep extends Base
{
    /**
     * [index 账目明细]
     * @param
     * @return json
     * @author [C。]
     */
    public function outranking()
    {
        $param = input('param.');
        $model = new AccountKeepModel;
        $acmodel = new AccountCateModel;
        try {
            $moren = $acmodel->getAllData(['comid'=>0,'status'=>1],'id','id,name,image','asc');
            $cate = $acmodel->getAllData(['comid'=>$param['comid'],'status'=>1],'id','id,name,image','asc');
            $cate = array_merge($moren,$cate);
            foreach($cate as &$v){
                $account_keep = $model->getSum(['acid'=>$v['id'],'comid'=>$param['comid'],'type'=>2],'price');
                $v['order_price'] = $account_keep;
            }
            $last_names = array_column($cate,'order_price');
            array_multisort($last_names,SORT_DESC,$cate);
            foreach($cate as &$v){
                if($cate[0]['order_price'] == 0){
                    $v['percent'] = 0;
                }else{
                    $v['percent'] = round(($v['order_price'] / $cate[0]['order_price'])*100,2);
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountkeep/outranking');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success', $cate);
    }

    /**
     * [index 账目明细]
     * @param
     * @return json
     * @author [C。]
     */
    public function getranking()
    {
        $param = input('param.');
        $model = new AccountKeepModel;
        $acmodel = new AccountCateModel;
        $omodel = new CompanyGongdengOrderModel;
        try {
            $moren = $acmodel->getAllData(['comid'=>0,'status'=>1],'id','id,name,image','asc');
            $cate = $acmodel->getAllData(['comid'=>$param['comid'],'status'=>1],'id','id,name,image','asc');

            if(empty($cate)){
                $cate = $moren;
            }else{
                $cate = array_merge($moren,$cate);
            }
            foreach($cate as &$v){
                if($v['id'] == 1 || $v['id'] == 3 || $v['id'] == 4 || $v['id'] == 5 || $v['id'] == 6 || $v['id'] == 7 || $v['id'] == 8){
                    if($v['id'] == 1){
                        $type = 4;
                    }elseif($v['id'] == 3){
                        $type = 3;
                    }elseif($v['id'] == 4){
                        $type = 7;
                    }elseif($v['id'] == 5){
                        $type = 5;
                    }elseif($v['id'] == 6){
                        $type = 1;
                    }elseif($v['id'] == 7){
                        $type = 9;
                    }elseif($v['id'] == 8){
                        $type = 6;
                    }
                    $order_price = $omodel->getSum(['type'=>$type,'status'=>1,'comid'=>$param['comid']],'total_price');
                    $account_keep = $model->getSum(['acid'=>$v['id'],'comid'=>$param['comid'],'type'=>1],'price');
                    $v['order_price'] = $order_price + $account_keep;
                }else{
                    $account_keep = $model->getSum(['acid'=>$v['id'],'comid'=>$param['comid'],'type'=>1],'price');
                    $v['order_price'] = $account_keep;
                }
            }

            $last_names = array_column($cate,'order_price');
            array_multisort($last_names,SORT_DESC,$cate);
            foreach($cate as &$v){
                if($cate[0]['order_price'] == 0){
                    $v['percent'] = 0;
                }else{
                    $v['percent'] = round(($v['order_price'] /array_sum($last_names)) ,2)*100;
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountkeep/getranking');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success', $cate);
    }
    /**
     * [index 账目明细]
     * @param
     * @return json
     * @author [C。]
     */
    public function account()
    {
        $param = input('param.');
        $model = new AccountKeepModel;
        $omodel = new CompanyGongdengOrderModel;
        try {
            $next_year = $param['year'] + 1;
            $all_get_order_price = $omodel->getSum(['status'=>1,'comid'=>$param['comid'],'create_time'=>['between',[strtotime($param['year'].'-1-1'),strtotime($next_year.'-1-1')]]],'total_price');
            $all_get_account = $model->getSum(['comid'=>$param['comid'],'type'=>1,'account_time'=>['between',[strtotime($param['year'].'-1-1'),strtotime($next_year.'-1-1')]]],'price');
            $all_out_account = $model->getSum(['comid'=>$param['comid'],'type'=>2,'account_time'=>['between',[strtotime($param['year'].'-1-1'),strtotime($next_year.'-1-1')]]],'price');
            $data['all_get_account'] = $all_get_order_price + $all_get_account;
            $data['all_out_account'] = $all_out_account;
            $data['all_residue_account'] = $data['all_get_account'] - $all_out_account;
            $i=0;
            for($i;$i<12;$i++){
                $j = $i +1;
                $days = $this->getMonthLastDay($i+1,$param['year']);
                $start = strtotime($param['year'].'-'.$j.'-1');
                $month_get_order_price = $omodel->getSum(['status'=>1,'comid'=>$param['comid'],'create_time'=>['between',[$start,$start+$days*24*3600]]],'total_price');
                $month_get_account = $model->getSum(['comid'=>$param['comid'],'type'=>1,'account_time'=>['between',[$start,$start+$days*24*3600]]],'price');
                $month_out_account = $model->getSum(['comid'=>$param['comid'],'type'=>2,'account_time'=>['between',[$start,$start+$days*24*3600]]],'price');
                $data['all_month_account'][$j]['month'] = $j;
                $data['all_month_account'][$j]['month_get_account'] = $month_get_order_price + $month_get_account;
                $data['all_month_account'][$j]['month_out_account'] = $month_out_account;
                $data['all_month_account'][$j]['month_residue_account'] = $data['all_month_account'][$j]['month_get_account'] - $month_out_account;
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountkeep/account');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success',$data);
    }
    /**
     * [index 账目明细]
     * @param
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new AccountKeepModel;
        $omodel = new CompanyGongdengOrderModel;
        $map = [];
        $data = [];
        try {
            $i = 0;
            $days = $this->getMonthLastDay($param['month'],$param['year']);
            $map['c.status'] = 1;
            $map['c.comid'] = $param['comid'];
            $month_start = strtotime($param['year'].'-'.$param['month'].'-1');
            $all_get_order_price = $omodel->getSum(['status'=>1,'comid'=>$param['comid'],'create_time'=>['between',[$month_start,$month_start+$days*24*3600]]],'total_price');
            $all_get_account_price = $model->getSum(['comid'=>$param['comid'],'type'=>1,'account_time'=>['between',[$month_start,$month_start+$days*24*3600]]],'price');
            $all_out_account_price = $model->getSum(['comid'=>$param['comid'],'type'=>2,'account_time'=>['between',[$month_start,$month_start+$days*24*3600]]],'price');
            $data['all_get_account_price'] = $all_get_order_price+$all_get_account_price;
            $data['all_out_account_price'] = $all_out_account_price;
            for($i;$i<$days;$i++){
                $j = $i +1;
                $week = get_week($param['year'].'-'.$param['month'].'-'.$j);
                $start = strtotime($param['year'].'-'.$param['month'].'-'.$j);
                $map['c.create_time'] = ['between',[$start,$start+24*3600]];
                $order = $omodel->getall($map,'c.gongdeng,c.total_price,c.type,m.tel,m.username,m.img');
                $order_price = $omodel->getSum(['status'=>1,'comid'=>$param['comid'],'create_time'=>['between',[$start,$start+24*3600]]],'total_price');
                $account = $model->getall(['c.comid'=>$param['comid'],'c.account_time'=>['between',[$start,$start+24*3600-1]]],'c.account_name,ac.name,c.price,c.type,m.tel,m.username,m.img');
                $get_account_price = $model->getSum(['comid'=>$param['comid'],'type'=>1,'account_time'=>['between',[$start,$start+24*3600-1]]],'price');
                $out_account_price = $model->getSum(['comid'=>$param['comid'],'type'=>2,'account_time'=>['between',[$start,$start+24*3600-1]]],'price');
                $data[$param['month'].'月'.$j.'日'.' '.$week] = ['order'=>$order,'account'=>$account,'get_account'=>$order_price+$get_account_price,'out_account'=>$out_account_price];
            }
            $data = array_reverse($data);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountkeep/index');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success',$data);
    }
    /**
     * [add 添加]
     * @param
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new AccountKeepModel;
        $user = $this->user;
        try {
            $param['account_time'] = strtotime($param['account_time']);
            $param['mid'] = $user['id'];
            $model->addData($param);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountkeep/add');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success');
    }
    /**
     * [getMonthLastDay 获取天数]
     * @param
     * @return json
     * @author [C。]
     */
    function getMonthLastDay($month, $year) {
        switch ($month) {
            case 4 :
            case 6 :
            case 9 :
            case 11 :
                $days = 30;
                break;
            case 2 :
                if ($year % 4 == 0) {
                    if ($year % 100 == 0) {
                        $days = $year % 400 == 0 ? 29 : 28;
                    } else {
                        $days = 29;
                    }
                } else {
                    $days = 28;
                }
                break;
            default :
                $days = 31;
                break;
        }
        return $days;
    }
}